package com.yixin.hospital.controller;

import cn.hutool.core.stream.CollectorUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yixin.hospital.common.ApiResult;
import com.yixin.hospital.common.Constant;
import com.yixin.hospital.entity.domain.JobOffice;
import com.yixin.hospital.entity.domain.User;
import com.yixin.hospital.entity.dto.req.joboffice.JobOfficeAddReqDTO;
import com.yixin.hospital.entity.dto.req.joboffice.JobOfficeEditReqDTO;
import com.yixin.hospital.enums.ResponseCodeEnum;
import com.yixin.hospital.service.JobOfficeService;
import com.yixin.hospital.service.UserService;
import com.yixin.hospital.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/job-office")
@Api(value = "科室管理", tags = "科室管理")
@Slf4j
public class JobOfficeController {

    @Resource
    JobOfficeService jobOfficeService;

    @Resource
    UserService userService;

    @GetMapping("/list")
    @ApiOperation(value = "科室列表")
    public ApiResult list(@RequestParam(value = "userId", required = false) Long userId) {
        if (Objects.isNull(userId)) return ApiResult.success(jobOfficeService.list());
        User user = userService.getById(userId);
        if (user.getJobOffice() != null) {
            List<Long> jobOfficeIdList = Arrays.asList(user.getJobOffice().split(",")).stream().map(Long::valueOf).collect(Collectors.toList());
            return ApiResult.success(jobOfficeService.list(new LambdaQueryWrapper<JobOffice>().in(JobOffice::getId, jobOfficeIdList)));
        }
        return ApiResult.success();
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加科室")
    public ApiResult add(@Valid @RequestBody JobOfficeAddReqDTO jobOfficeAddReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        List<JobOffice> list = jobOfficeService.list(new LambdaQueryWrapper<JobOffice>().eq(JobOffice::getName, jobOfficeAddReqDTO.getName()));
        if (!CollectionUtils.isEmpty(list)) return ApiResult.error(ResponseCodeEnum.JOB_OFFICE_EXIST);
        JobOffice jobOffice = new JobOffice();
        jobOffice.setName(jobOfficeAddReqDTO.getName());
        jobOffice.setCreatedBy(userName);
        jobOffice.setUpdatedBy(userName);
        jobOfficeService.save(jobOffice);
        return ApiResult.success();
    }

    @PutMapping("/edit")
    @ApiOperation(value = "编辑科室")
    public ApiResult edit(@Valid @RequestBody JobOfficeEditReqDTO jobOfficeEditReqDTO, HttpServletRequest request) {
        String userName = JwtUtil.getName(request.getHeader(Constant.AUTHORIZATION));
        JobOffice jobOffice = new JobOffice();
        jobOffice.setId(jobOfficeEditReqDTO.getId());
        jobOffice.setName(jobOfficeEditReqDTO.getName());
        jobOffice.setCreatedBy(userName);
        jobOffice.setUpdatedBy(userName);
        jobOfficeService.updateById(jobOffice);
        return ApiResult.success();
    }

    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "删除科室")
    public ApiResult remove(@PathVariable int id) {
        jobOfficeService.removeById(id);
        return ApiResult.success();
    }
}
